package com.wangy.service.impl;

import com.wangy.service.ExecuteSQLService;
import org.springframework.stereotype.Service;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;

/**
 * @author yyzb
 * @date 2024/12/30 17:40
 */
@Service
public class ExecuteSQLServiceImpl implements ExecuteSQLService {

    @Override
    public String executeMySQL(String sql) {
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/gmall_report?useSSL=false&serverTimezone=UTC&allowPublicKeyRetrieval=true", "root", "123456");
            PreparedStatement preparedStatement = conn.prepareStatement(sql);
            preparedStatement.execute();
            preparedStatement.close();
            conn.close();
            return "success execute sql!";
        }catch(Exception e) {
            e.printStackTrace();
            return "error execute sql!";
        }
    }

    @Override
    public String executeClickhouse(String sql) {
        try {
            Connection conn = DriverManager.getConnection("jdbc:clickhouse://localhost:8123/default?useSSL=false");
            PreparedStatement preparedStatement = conn.prepareStatement(sql);
            preparedStatement.execute();
            preparedStatement.close();
            conn.close();
            return "success execute sql!";
        }catch(Exception e) {
            e.printStackTrace();
            return "error execute sql!";
        }
    }
}
